

-- Namespace
	local _,mod = ...


-- Object reference
	local units = mod.units
	local resolve = mod.resolve


-- Threat
	local care = mod:unitevent_care('threat')
	if care then
		local threattbl = {}
		local paused = true
		
		local function threattimer()
			for unit,u in pairs(threattbl) do
				local tanking,status,pthreatpct,rthreatpct,rthreat = UnitDetailedThreatSituation(unit,'target')
				if pthreatpct then
					u:Update('threat',true,tanking,status,pthreatpct,rthreatpct,rthreat)
				else
					u:Update('threat',nil)
				end
			end
		end
		
		local function THREAT(unit)
			if not care[unit] then return end
			local u = units[unit]
		
			local targetexists = UnitExists('target')
			local targetplayer = UnitPlayerControlled('target')
			local targetfriend = UnitIsFriend('target','player')

			if targetexists and not targetplayer and not targetfriend then
				-- We've got a unit to track threat against!
				if UnitExists(unit) and UnitIsFriend(unit,'player') then  -- Is this running on a friendly, existent frame?
					threattbl[unit] = u  -- If so, begin tracking.
				else
					threattbl[unit] = nil  -- If not, false alarm. Abort.
					return
				end
				if paused then  -- We weren't previously tracking, so start.
					mod:timer_register(.5,true,threattimer)()  -- 'lp' = throttled to 2 Hz (.5 sec).
					paused = false
				end
			else
				-- We no longer have any units to track threat against.
				mod:timer_unregister(threattimer)
				paused = true
				wipe(threattbl)
				u:Update('threat',nil)  -- Send an empty update so the frame knows we're no longer tracking.
			end
		end
		
		mod:event_register('UNIT_THREAT_SITUATION_UPDATE',THREAT)
		mod:event_register('UNIT_THREAT_LIST_UPDATE',function(unit)
			if unit == 'target' then
				for unit in pairs(units) do
					if care[unit] then THREAT(unit) end
				end
			end
		end)
		mod:event_register('PLAYER_TARGET_CHANGED',function()
			for unit in pairs(units) do
				if care[unit] then THREAT(unit) end
			end
		end)
	end
